/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.differentCardType;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.hardCode.CardType;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractDifferentLogic;
import com.dc.gameserver.serverCore.service.cardModule.Card;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 石头哥哥</br>
 *         Project : gameServer1.1</br>
 *         Date: 13-11-2  11</br>
 *         Time: 下午4:47</br>
 *         Connect: 13638363871@163.com</br>
 *         packageName: Server.ServerCore.Service.fightProcess.differentCardType</br>
 *         注解：34  攻击+技能
 */
@Service
public class d_attack_magic extends AbstractDifferentLogic {


    @Override
    public void PostConstruct() {
        IDIFFERENTS[Integer.parseInt(CardType.ATTACK_TYPE.getType()+""+CardType.MAGIC_TYPE.getType())]=this;
    }

    /**
     * 处理不同类型牌的逻辑
     * @param p1   优先级高的玩家
     * @param p2   优先级低的玩家
     *             攻击 +技能  比点数
     */
    @Override
    public void differentLogicHandle(GameCharacter p1,GameCharacter p2) {
        CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
        //比点数规则
        if (Config.DEFAULT_VALUE.GAME_VALUE.rulePoint){
            //当技能的点数大于攻击点数有效；
            List<Card> outAttackList=p1.getOutCardsList();
            List<Card> outMagicList=p2.getOutCardsList();
            int attackCount=0,magicCount=0;
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:outAttackList){
                attackCount+=card.getCount();
            }
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:outMagicList){
                magicCount+=card.getCount();
            }
            if (attackCount>magicCount){
                //攻击有效        p1--attack--->p2
                //p1--attack-->p2
                int garms= GameFormula.grams(p1.getSelfFiveType(), p2.getSelfFiveType());
                int hurt=0;
//                int p2Blood=p2.getCurrentBlood();
                int p2Shield=p2.getCurrentShield();
                int p2Defence=p2.getDefence();
                int p1Attack=p1.getAttack();
                if (garms== 0x1){
                    hurt= GameFormula.attackHurtWithGrams(p2Defence, p1Attack);
                }else if (garms== 0x2){
                    hurt=GameFormula.attackHurtWithUnGrams(p2Defence, p1Attack);
                } else {
                    hurt=GameFormula.attackHurtWithGramsed(p2Defence, p1Attack);
                }
//                if (hurt==0){
//                    throw  new logicException("please check logic, hurt is 0 ... ...");
//                }
                //p1对p2进行物理攻击
//                if (p2Shield>hurt){
//                    p2.setCurrentShield(p2Shield-hurt);
//                } else {
//                    if (p2Shield==0){
//                        p2.setCurrentBlood(p2Blood-hurt);
//                    }else {
//                        p2.setCurrentBlood(p2Blood+p2Shield-hurt);
//                        p2.setCurrentShield(0);  //next time will be 0
//                    }
//                }
                p2.setCurrentShield(p2Shield-hurt);
                cardBox.sendHurtRule(p2.getID(),hurt,p1.getBattleSkillID());
                if (p2.getCurrentShield()<=0){
                    p2.setCurrentShield(0);
//                    p2.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p1);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }

            }else if (attackCount<magicCount){
                //魔法有效    p2---magic-->p1
                //p1 hurt ,p2 active
                int garms= GameFormula.grams(p2.getSkillfiveType(), p1.getSelfFiveType());
                int p1Hurt=0;  //p1 造成的伤害
                int p1Shield=p1.getCurrentShield();
//                int p1Blood=p1.getCurrentBlood();
                int p1Defence=p1.getDefence();
                int p2FiveTypeSkillValue=p2.getFiveTypeSkillValue();
                if (garms== 0x1){
                    p1Hurt=GameFormula.magicHurtWithGrams(p1Defence, p2FiveTypeSkillValue);
                }else if (garms== 0x2){
                    p1Hurt=GameFormula.magicHurtWithUnGrams(p1Defence, p2FiveTypeSkillValue);
                } else {
                    p1Hurt=GameFormula.magicHurtWithGramsed(p1Defence, p2FiveTypeSkillValue);
                }
                if (p1Hurt<=0){
                    p1Hurt=0;
                    p1Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
                }

                //p2 法力值消耗
                int p2Magic=GameFormula.computeMagicUse(p2); // notify
                if (p2Magic!=0){
                    p2.setCurrentMagic(p2.getCurrentMagic()-p2Magic);
                }

//                if (p1Shield>p1Hurt){
//                    p1.setCurrentShield(p1Shield-p1Hurt);
//                } else {
//                    if (p1Shield==0){
//                        p1.setCurrentBlood(p1Blood-p1Hurt);
//                    }else {
//                        p1.setCurrentBlood(p1Blood+p1Shield-p1Hurt);
//                        p1.setCurrentShield(0);
//                    }
//                }

                p1.setCurrentShield(p1Shield-p1Hurt);
                cardBox.sendHurtRule(p1.getID(),p1Hurt,p2.getBattleSkillID());
                if (p1.getCurrentShield()<=0){
                    p1.setCurrentShield(0);
//                    p1.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p2);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    //点数相同
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }

            } else if (attackCount==magicCount){
                //flush blood    attack
                cardBox.flushBloodAndMagic();
                cardBox.resetRoundResource();
            }
        }else {
            //p1--attack--->p2
            int garms= GameFormula.grams(p1.getSelfFiveType(), p2.getSelfFiveType());
            int hurt=0;
//            int p2Blood=p2.getCurrentBlood();
            int p2Shield=p2.getCurrentShield();
            int p2Defence=p2.getDefence();
            int p1Attack=p1.getAttack();
            if (garms== 0x1){
                hurt= GameFormula.attackHurtWithGrams(p2Defence, p1Attack);
            }else if (garms== 0x2){
                hurt=GameFormula.attackHurtWithUnGrams(p2Defence, p1Attack);
            } else {
                hurt=GameFormula.attackHurtWithGramsed(p2Defence, p1Attack);
            }
            if (hurt<=0){
                hurt=0;
                hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
            }


            //p1对p2进行物理攻击
//            if (p2Shield>hurt){
//                p2.setCurrentShield(p2Shield-hurt);
//            } else {
//                if (p2Shield==0){
//                    p2.setCurrentBlood(p2Blood-hurt);
//                }else {
//                    p2.setCurrentBlood(p2Blood+p2Shield-hurt);
//                    p2.setCurrentShield(0);  //next time will be 0
//                }
//            }
            //同步伤害
            p2.setCurrentShield(p2Shield-hurt);
            cardBox.sendHurtRule(p2.getID(),hurt,p1.getBattleSkillID());
            //魔法有效    p2---magic-->p1
            //p1 hurt ,p2 active
            garms= GameFormula.grams(p2.getSkillfiveType(), p1.getSelfFiveType());
            int p1Hurt=0;  //p1 造成的伤害
            int p1Shield=p1.getCurrentShield();
//            int p1Blood=p1.getCurrentBlood();
            int p1Defence=p1.getDefence();
            int p2FiveTypeSkillValue=p2.getFiveTypeSkillValue();

            if (garms== 0x1){
                p1Hurt=GameFormula.magicHurtWithGrams(p1Defence, p2FiveTypeSkillValue);
            }else if (garms== 0x2){
                p1Hurt=GameFormula.magicHurtWithUnGrams(p1Defence, p2FiveTypeSkillValue);
            } else {
                p1Hurt=GameFormula.magicHurtWithGramsed(p1Defence, p2FiveTypeSkillValue);
            }
            if (p1Hurt<=0){
                p1Hurt=0;
                p1Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
            }

            //p2 法力值消耗
            int p2Magic=GameFormula.computeMagicUse(p2); // notify
            if (p2Magic!=0){
                p2.setCurrentMagic(p2.getCurrentMagic()-p2Magic);
            }

//            if (p1Shield>p1Hurt){
//                p1.setCurrentShield(p1Shield-p1Hurt);
//            } else {
//                if (p1Shield==0){
//                    p1.setCurrentBlood(p1Blood-p1Hurt);
//                }else {
//                    p1.setCurrentBlood(p1Blood+p1Shield-p1Hurt);
//                    p1.setCurrentShield(0);
//                }
//            }

            p1.setCurrentShield(p1Shield-p1Hurt);
            cardBox.sendHurtRule(p1.getID(),p1Hurt,p2.getBattleSkillID());
            if (p1.getCurrentShield()<=0){
                p1.setCurrentShield(0);
//                p1.setCurrentBlood(0);
                //被攻击掉血
                cardBox.flushBloodAndMagic();
                cardBox.notifyPkResult(p2);
                CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                return;
            }

            if (p2.getCurrentShield()<=0){
                p2.setCurrentShield(0);
//                p2.setCurrentBlood(0);
                //被攻击掉血
                cardBox.flushBloodAndMagic();
                cardBox.notifyPkResult(p1);
                CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                return;
            }

            //flush blood    attack
            cardBox.flushBloodAndMagic();
            cardBox.resetRoundResource();
        }
    }
}
